<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./lib/swiper/swiper.css">
  <style>
    #swiper{
      width: 800px;
      overflow: hidden;
      margin: 10px auto;
    }
    #swiper img{
      width: 100%;
    }
    .swiper-slide{
      width: 800px;
      height: 600px;
      background-color: aqua;
      font-size: 100px;
    }
    .swiper-slide:nth-child(2n){
      background-color: rgb(94, 34, 143);
    }
  </style>
</head>

<body>
  <div id="app">
    <home></home>
  </div>
  <script src="./vue.js"></script>
  <script src="./lib/swiper/swiper.js"></script>
  <script>

    const Home = {
      template: `
        <div>
          <h2>这是一个组件</h2>  
          <button @click.once="addSwiper">动态添加一张图片</button>
          <div class="swiper" id="swiper">
            <div class="swiper-wrapper">
              <div v-for="(banner,index) in banners" :key="index" class="swiper-slide">
                  {{banner}}
              </div>
            </div>
          </div>
        </div>
      `,
      data() {
        return {
          swiper: null,
          banners: [
            1,
            2,
            3,
            4
          ]
        }
      },
      mounted(){
        this.initSwiper();
      },
      methods: {
        addSwiper(){
          // 添加一个轮播向
          this.banners.push(5);
          this.$nextTick(() => {
            // this.initSwiper();
            this.swiper.update();
          })
        },
        initSwiper(){
          this.swiper = new Swiper('#swiper', {
          })
        }
      }
    }

    Vue.component('Home', Home)
    const vm = new Vue({
      el: '#app',
      data: {
        isShow: true
      }
    })
  </script>
</body>

</html>